<?php

namespace app\common\model;

use app\common\model\account\ShopAccountModel;
use think\facade\Db;


class SupplierGoodsModel extends CommonModel{

    protected  $name = 'beautiful_supplier_goods';

    protected  $pk   = 'id';


    public static function info($where){
        $info = Db::name('beautiful_supplier_goods')->alias('a')
            ->join('beautiful_goods b','a.goods_id=b.goods_id')
            ->join('beautiful_goods_category c','b.category_id=b.category_id')
            ->where(['a.is_delete'=>0])
            ->when(!empty($where['shop_id']),function ($query) use ($where){
                $query->where(['a.shop_id'=>$where['shop_id']]);
            })
            ->when(!empty($where['id']),function ($query) use ($where){
                $query->where(['a.id'=>$where['id']]);
            })
            ->when(!empty($where['supplier_id']),function ($query) use ($where){
                $query->where(['a.supplier_id'=>$where['supplier_id']]);
            })
            ->when(isset($where['is_effect']),function ($query) use ($where){
                $query->where(['a.is_effect'=>$where['is_effect']]);
            })
            ->when(!empty($where['name']),function ($query) use ($where){
                $query->where('a.name','like','%'.$where['name'].'%');
            })
            ->field("a.*,b.name as goods_name,b.crossed_price,c.name as category_name")
            ->find();
        return $info;
    }
    public static function supllierInfo($where,$field='*'){
        $info = Db::name('beautiful_supplier_goods')->alias('sg')
            ->join('beautiful_supplier s','s.id=sg.supplier_id')
            ->where(['sg.is_delete'=>0])
            ->when(!empty($where['goods_id']),function ($query) use ($where){
                $query->where(['sg.goods_id'=>$where['goods_id']]);
            })
            ->field($field)
            ->find();
        return $info;
    }

    public static function list($where,$order=['a.id'=>'desc']){
        $query = Db::name('beautiful_supplier_goods')->alias('a')
            ->join('beautiful_goods b','a.goods_id=b.goods_id')
            ->join('beautiful_goods_category c','b.category_id=c.category_id')
            ->leftJoin('beautiful_goods_other_sku_detail d','b.unit_id=d.other_sku_detail_id')
            ->where(['a.is_delete'=>0])
            ->when(!empty($where['shop_id']),function ($query) use ($where){
                $query->where(['a.shop_id'=>$where['shop_id']]);
            })
            ->when(isset($where['is_effect']),function ($query) use ($where){
                $query->where(['a.is_effect'=>$where['is_effect']]);
            })
            ->when(!empty($where['supplier_id']),function ($query) use ($where){
                $query->where(['a.supplier_id'=>$where['supplier_id']]);
            })
            ->when(!empty($where['name']),function ($query) use ($where){
                $query->where('a.name','like','%'.$where['name'].'%');
            })
            ->field("a.*,b.name as goods_name,b.crossed_price,b.category_id,c.name as category_name,d.group_detail_name as unit_name");
        $list=$query->order($order)
            ->select()
            ->toArray();
        return $list;
    }
    public static function up($data,$where){
        $data['edit_time']=date('Y-m-d H:i:s');
        $res=self::update($data,$where);
        return $res;
    }
    public static function getById($where,$field='*')
    {
        $list = Db::name('beautiful_supplier_goods')->where($where)->field($field)->find();

        return $list;
    }
}

